package com.edu.demo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.edu.demo.entity.Conclusion;
import com.edu.demo.entity.MediumTerm;
import com.edu.demo.pojo.AjaxResult;
import com.edu.demo.service.IConclusionService;
import com.edu.demo.service.IMediumTermService;
import com.edu.demo.service.IReportService;
import com.edu.demo.service.impl.MediumTermServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**周期检查报告
 * @ClassName: MediumTermController
 * @Description:
 * @Author: jxw
 * @Date: 2023-03-10
 */
@RestController
@RequestMapping("/medium")
public class MediumTermController {


    @Autowired
    private IMediumTermService mediumTermService;

    @Autowired
    private IReportService reportService;

    @Autowired
    IConclusionService conclusionService;

    /**
     * 学生提交中期报告
     * @param mediumTerm
     * @return
     */
    @PostMapping("/submit/medium")
    @PreAuthorize("@ps.hasPerm('stu:project:submit')")
    public AjaxResult submitMediumTerm(@RequestBody MediumTerm mediumTerm){
        mediumTerm.setUploadTime(new Date());
        //提交中期报告
        try {
            mediumTermService.submitMedium(mediumTerm);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error("没有该项目");
        }
        return AjaxResult.success("提交成功");
    }

    /**
     * 查看周期报告
     * @param project
     * @return
     */
    @GetMapping("/check")
    @PreAuthorize("@ps.hasPerm('common:medium:check')")
    public AjaxResult checkProjectMedium(@RequestParam Integer project){
        try {
            MediumTerm mediumTerm = mediumTermService.selectMedium(project);
            return AjaxResult.success(mediumTerm);
        }catch (BadCredentialsException e) {
            e.printStackTrace();
            return AjaxResult.error(e.getMessage());

        }
    }


    /**
     * 学生更新修改周期报告
     * @param mediumTerm
     * @return
     */
    @PostMapping("/update")
    @PreAuthorize("@ps.hasPerm('stu:medium:update')")
    public AjaxResult updateMedium(@RequestBody MediumTerm mediumTerm){
        int i = mediumTermService.updateMedium(mediumTerm);
        return AjaxResult.success(i==1);
    }


    /**
     * 教师将周期报告修改为已阅
     * @param mediumId
     * @return
     */
    @GetMapping("/checked")
    @PreAuthorize("@ps.hasPerm('tea:medium:update')")
    public AjaxResult checkedMedium(@RequestParam Integer mediumId){
        MediumTerm term = new MediumTerm();
        term.setId(mediumId);
        int i = mediumTermService.checkedMedium(term);
        if(i==-22) return AjaxResult.error("只能修改自己的项目");
        return AjaxResult.success(i==1);
    }

    /**
     * 学生工作台页面的：提交周报告数、是否提交中期报告、是否提交结题报告
     * @param pId
     * @return
     */
    @GetMapping("/count")
    @PreAuthorize("@ps.hasPerm('stu:medium:update')")
    public AjaxResult count(@RequestParam int pId){
        int countMine = reportService.countMine(pId);
        Map<String,Object> res = new HashMap<>();
        res.put("report",countMine);
        try {
            MediumTerm term = mediumTermService.selectMedium(pId);
            if(!Objects.isNull(term)){
                res.put("medium","已提交");
            }else {
                res.put("medium","待提交");
            }
        }catch (Exception e){
            res.put("medium","待提交");
        }
        List<Conclusion> conclusions = conclusionService.list(new QueryWrapper<Conclusion>().eq("project_id", pId));
        if(conclusions.isEmpty()){
            res.put("conclusion","待提交");
        }else {
            res.put("conclusion","已提交");
        }

        return AjaxResult.success(res);


    }
}
